package servlet;

import java.io.IOException;
import java.security.KeyStore.PrivateKeyEntry;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.bcel.internal.generic.AALOAD;
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.PrivateKeyResolver;

import dao.ArticleDAO;
import dao.ContentDAO;
import dao.ThemeDAO;
import entity.Article;
import entity.Content;
import entity.Theme;

/**
 * Servlet implementation class ModityArticleServlet
 */
@WebServlet("/BeforeModityArticleServlet")
public class BeforeModityArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ArticleDAO aticleDAO =  ArticleDAO.getInstance();
    private ContentDAO contentDAO = ContentDAO.getInstance();
    private ThemeDAO themeDAO = ThemeDAO.getInstance();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BeforeModityArticleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		int article_id = Integer.parseInt((String)request.getParameter("article_id"));
		Article article = aticleDAO.get(article_id);
		Content content = contentDAO.get(article_id);
		request.setAttribute("content", content.getContent());
		request.setAttribute("article", article);
		request.setAttribute("theme_name", themeDAO.get(article.getTheme_id()).getTheme_name());
		List<Theme> themelist = themeDAO.getAll();
		request.setAttribute("themelist", themelist);
		System.out.println(themelist.toString());
		String fromadmin = (String)request.getParameter("from");
		if(fromadmin!=null&&fromadmin.equals("admin")){//从后台来的请求
			if(request.getAttribute("type")!=null) {
				request.setAttribute("type", 2);
			}
			request.getRequestDispatcher("AdminArticleChange.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("ModifyArticleServlet.jsp").forward(request,response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
